# 博客
class Blog < ApplicationRecord
  validates :title, presence: {message: "标题不能为空"}
  validates :title, uniqueness: {message: "标题不能重复"}
  validates :content, presence: {message: "内容不能为空"}
  validates :user_id, presence: {message: "用户不能为空"}
  # 一个博客属于一个用户
  belongs_to :user
  # 一个博客有多个评论
  has_and_belongs_to_many :comments

  public

  def access_count
    return AccessLog.where("method = 'GET' and fullpath = '/blogs/#{self.id}'").count
  end
end
